<script setup>
import { onMounted, ref } from 'vue'
import dial from './dial.vue'
import { getAllstatusApi, delApi, updateApi } from '@/api/warehousingentry/warehousingEntryDetails'
import { addBatchApi } from '@/api/warehousingentry/warehousingEntryDetailsSort'
import { ElMessage } from 'element-plus'
import eventBus from '@/utils/eventBus'

const tableData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(true)
const disabled = ref(false)
const handleSizeChange = (val) => {
  pageSize.value = val
  getAll()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getAll()
}

const form = ref({
  skuId: null,
  warehousingEntryId: null
})


const getAll = async () => {
  let res = await getAllstatusApi(
    currentPage.value,
    pageSize.value
  )
  total.value = res.total
  tableData.value = res.rows
}

const diaRef = ref(null)
const search = () => {
  currentPage.value = 1
  getAll()
}

const ids = ref([])
const handleSelectionChange = (val) => {
  ids.value = val.map(item => item.id)
}
const getItemNameById = (skuId) => {
  if (!diaRef.value || !diaRef.value.itemInfo) {
    return '未知商品'
  }
  for (const item of diaRef.value.itemInfo) {
    if (item.id === skuId) {
      return item.title
    }
  }
  return '未知商品'
}
const getItemInfoById = (skuId) => {
  if (!diaRef.value || !diaRef.value.itemInfo) {
    return '未知商品'
  }
  for (const item of diaRef.value.itemInfo) {
    if (item.id === skuId) {
      return item.subtitle
    }
  }
  return '未知商品'
}
const handleArrive = () => {
  if (ids.value.length === 0) {
    ElMessage.warning('请至少选择一条记录')
    return
  }
  diaRef.value.selectedRows = tableData.value.filter(row => ids.value.includes(row.id))
  diaRef.value.dialogVisible = true
}

defineExpose({
  refreshData: getAll
})

onMounted(() => {
  getAll()
})
</script>

<template>
  <dial ref="diaRef" @refresh="getAll" />
  <div>
    <el-button @click="handleArrive" type="primary" style="float: left;">√</el-button>
  </div>
  <el-form :model="form" :inline="true" style="float: right;">
    <el-form-item>
      <el-input v-model="form.skuId" placeholder="规格ID" />
    </el-form-item>
    <el-form-item>
      <el-input v-model="form.warehousingEntryId" placeholder="入库单ID" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" />
    <el-table-column property="skuId" label="规格ID" />
    <el-table-column property="skuId" label="商品名称">
      <template #default="{ row }">
        {{ getItemNameById(row.skuId) }}
      </template>
    </el-table-column>
    <el-table-column property="skuId" label="规格名称">
      <template #default="{ row }">
        {{ getItemInfoById(row.skuId) }}
      </template>
    </el-table-column>
    <el-table-column property="count" label="数量" />
    <el-table-column property="supplierId" label="供应商ID" />
    <el-table-column property="price" label="单价(分)" />
    <el-table-column property="warehousingEntryId" label="入库单ID" />
    <el-table-column property="createTime" label="创建时间" />
  </el-table>

  <div class="demo-pagination-block">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40]"
      :size="size" :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>